package aygx.automatic.config;

import aygx.automatic.annotation.ConditionOnClass;
import aygx.automatic.condition.UserCondition;
import aygx.automatic.enrity.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * @author 暗影孤星
 * @date 2020/10/27 22:52
 * @explanation
 */
@Configuration
public class UserConfig {

    /**
     * @return user
     * @Conditional(Class<? extends Condition>[] value()) 满足数组内所有条件则创建bean 条件：Condition实现类的matches方法返回true
     */
    @Bean(name = "user")
    @Conditional({UserCondition.class})
    public User user() {
        return new User(1);
    }


    @Bean(name = "user1")
    @ConditionOnClass("aygx.automatic.enrity.User")
    public User user1() {
        return new User(2);
    }

    /**
     * 当 aygx=zxx 属性存在时创建bean
     * @return
     */
    @Bean(name = "user2")
    @ConditionalOnProperty(name = "aygx", havingValue = "zxx")
    public User user2() {
        return new User(3);
    }

}